package handling.channel.handler;

import client.MapleClient;
import handling.world.World.Guild;
import handling.world.guild.MapleBBSThread;
import tools.MaplePacketCreator;
import tools.data.LittleEndianAccessor;

import java.util.List;

public class BBSHandler {
    private static String correctLength(String in, int maxSize) {
        if (in.length() > maxSize) {
            return in.substring(0, maxSize);
        }
        return in;
    }

    public static void HandleBBS(LittleEndianAccessor slea, MapleClient c) {
        if (c.getPlayer().getGuildId() <= 0) {
            return;
        }
        try {
            int localThreadId = 0;
            int read = slea.readByte();
            BBSOperation action = BBSOperation.getByValue((byte) read);
            if (action != null) {

                switch (action) {
                    case ADD_THREAD: {
                        boolean isEdit = slea.readByte() > 0;
                        if (isEdit) {
                            localThreadId = slea.readInt();
                        }
                        boolean isNotice = slea.readByte() > 0;
                        String title = correctLength(slea.readMapleAsciiString(), 25);
                        String content = correctLength(slea.readMapleAsciiString(), 600);
                        int icon = slea.readInt();
                        if (icon >= 100 && icon <= 106) {
                            if (!c.getPlayer().haveItem(5290000 + icon - 100, 1, false, true)) {
                                return;
                            }
                        } else if (icon < 0 || icon > 2) {
                            return;
                        }
                        if (!isEdit) {
                            addNewBBSThread(c, title, content, icon, isNotice);

                        }
                        editBBSThread(c, title, content, icon, localThreadId);

                    }
                    case DELETE_THREAD: {
                        localThreadId = slea.readInt();
                        deleteBBSThread(c, localThreadId);
                    }
                    case LIST_THREAD: {

                        try {
                            int start = slea.readInt();
                            listBBSThreads(c, start * 10);
                        } catch (ArrayIndexOutOfBoundsException ex) {
                        }

                    }
                    case DISPLAY_THREAD: {

                        try {
                            localThreadId = slea.readInt();
                            displayThread(c, localThreadId);
                        } catch (ArrayIndexOutOfBoundsException ex2) {
                        }

                    }
                    case ADD_REPLY: {
                        localThreadId = slea.readInt();
                        String text = correctLength(slea.readMapleAsciiString(), 25);
                        newBBSReply(c, localThreadId, text);
                    }
                    case DELETE_REPLY: {
                        localThreadId = slea.readInt();
                        int replyid = slea.readInt();
                        deleteBBSReply(c, localThreadId, replyid);
                        break;
                    }
                    default: {
                        System.err.println("未处理的BBS动作: " + read);
                        break;
                    }
                }
            } else {
                System.err.println("未处理的BBS动作: " + read);
            }
        } catch (ArrayIndexOutOfBoundsException ex3) {
        }
    }

    private static void listBBSThreads(MapleClient c, int start) {
        c.sendPacket(MaplePacketCreator.BBSThreadList(Guild.getBBS(c.getPlayer().getGuildId()), start));
    }

    private static void newBBSReply(MapleClient c, int localthreadid, String text) {
        Guild.addBBSReply(c.getPlayer().getGuildId(), localthreadid, text, c.getPlayer().getId());
        displayThread(c, localthreadid);
    }

    private static void editBBSThread(MapleClient c, String title, String text, int icon, int localthreadid) {
        Guild.editBBSThread(c.getPlayer().getGuildId(), localthreadid, title, text, icon, c.getPlayer().getId(), (int) c.getPlayer().getGuildRank());
        displayThread(c, localthreadid);
    }

    private static void addNewBBSThread(MapleClient c, String title, String text, int icon, boolean bNotice) {
        displayThread(c, Guild.addBBSThread(c.getPlayer().getGuildId(), title, text, icon, bNotice, c.getPlayer().getId()));
    }

    private static void deleteBBSThread(MapleClient c, int localthreadid) {
        Guild.deleteBBSThread(c.getPlayer().getGuildId(), localthreadid, c.getPlayer().getId(), (int) c.getPlayer().getGuildRank());
    }

    private static void deleteBBSReply(MapleClient c, int localthreadid, int replyid) {
        Guild.deleteBBSReply(c.getPlayer().getGuildId(), localthreadid, replyid, c.getPlayer().getId(), (int) c.getPlayer().getGuildRank());
        displayThread(c, localthreadid);
    }

    private static void displayThread(MapleClient c, int localthreadid) {
        List<MapleBBSThread> bbsList = Guild.getBBS(c.getPlayer().getGuildId());
        if (bbsList != null) {
            for (MapleBBSThread t : bbsList) {
                if (t != null && t.localthreadID == localthreadid) {
                    c.sendPacket(MaplePacketCreator.showThread(t));
                }
            }
        }
    }

    enum BBSOperation {
        ADD_THREAD((byte) 0),
        DELETE_THREAD((byte) 1),
        LIST_THREAD((byte) 2),
        DISPLAY_THREAD((byte) 3),
        ADD_REPLY((byte) 4),
        DELETE_REPLY((byte) 5);

        byte value;

        private BBSOperation(byte value) {
            this.value = -1;
            this.value = value;
        }

        public int getValue() {
            return this.value;
        }

        public static BBSOperation getByValue(byte value) {
            for (BBSOperation o : values()) {
                if (o.getValue() == value) {
                    return o;
                }
            }
            return null;
        }
    }
}
